package com.dm.shop.service.impl;

import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.entity.ShopCart;
import com.dm.shop.mapper.ShopCartMapper;
import com.dm.shop.service.ShopCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.shop.utils.CommonUtil;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@Service
public class ShopCartServiceImpl extends ServiceImpl<ShopCartMapper, ShopCart> implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    /**
     * 根据会员编号得到用户的购物车
     * @param uid
     * @return
     */
    @Override
    public Result getShopCartInfo(int uid) {
        List<Map> shopCartInfoList=shopCartMapper.getShopCartInfo(uid);
        if(shopCartInfoList!=null){
            return ResultUtil.success(shopCartInfoList);
        }else {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    /**
     * 删除某一个用户购物车中的某个商品
     * @param uid
     * @param pid
     * @return
     */
    @Override
    public Result delShopCartById(int uid, int pid) {
        System.out.println("到这里还有啊吗？"+uid+"-------------"+pid);
        boolean IsDel=shopCartMapper.delShopCartById(uid,pid);
        if(IsDel!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    /**
     * 清空购物车
     * @param uid
     * @return
     */
    @Override
    public Result delAllShopCart(int uid) {
        boolean IsDel=shopCartMapper.delAllShopCart(uid);
        if(IsDel!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    /**
     *
     * @param shopCart
     * @return
     */
    @Override
    public Result addShopCart(ShopCart shopCart) {
        boolean IsSave=this.save(shopCart);
        if(IsSave!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }
}
